package com.graduation.rescuematerial.controller.system;

import com.graduation.rescuematerial.service.ICatalogService;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/29 1:38
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/sys/catalog")
@Api(tags = "系统管理员——物资目录模块")
public class SysCatalogController {

    @Autowired
    private UserService userService;
    @Autowired
    private ICatalogService catalogService;

    @ApiOperation("获取物资目录列表")
    @PostMapping("/get/all")
    @ResponseBody
    public RespBean getAllCatalog(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return catalogService.getAllCatalog();
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }


    @ApiOperation("添加物资目录")
    @PostMapping("/add")
    @ResponseBody
    public RespBean addOne(@RequestBody CatalogVo addCatalogVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=addCatalogVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return catalogService.addOne(addCatalogVo);
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("修改物资目录")
    @PostMapping("/update")
    @ResponseBody
    public RespBean updateInfo(@RequestBody CatalogVo addCatalogVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=addCatalogVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return catalogService.updateInfo(addCatalogVo);
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("/模糊查询目录")
    @PostMapping("/fuzzy")
    @ResponseBody
    public RespBean fuzzyQuery(@RequestBody FuzzyVo fuzzyVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=fuzzyVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return catalogService.fuzzyQuery(fuzzyVo.getKey());
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }
}
